package com.sdry.web.controller.llm;

import java.net.InetAddress;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.model.llm.Department;
import com.sdry.model.llm.LogMana;
import com.sdry.model.llm.User;
import com.sdry.service.llm.LogManaService;

/** 
* 拦截器
* @author lian 
* @version 1.0
* @time 2018年10月16日 下午2:06:00
* 记录信息: 访问时间-对应方法名-ip地址-用户名-结果
*/
public class LogInterceptor implements HandlerInterceptor{
	
	@Resource
	private LogManaService logManaService;  //注入日志service
	
	//日志信息实体类
	LogMana log = new LogMana();
	
	/**
	 * 这个方法在整个请求执行之后，在视图渲染之前调用，一般做资源清理工作
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)
			throws Exception {
		
	}
	
	/**
	 * 这个方法在controller请求之后调用
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
		if (handler instanceof HandlerMethod) {
			String uri = request.getRequestURI();
			
		
			User	 people = (User) request.getSession().getAttribute("currentUser");
			Department dep = (Department)request.getSession().getAttribute("department");
		
			
    		//登出的Controller执行之后
    		if("/logout.do".equals(uri) || "/appLogout.do".equals(uri)){
    			request.getSession().removeAttribute("currentUser");
    			request.getSession().removeAttribute("department");
    			request.getSession().removeAttribute("team");
    		}else{
    			//判断session中的用户和部门信息
    			if(null != people && null != dep){
    				log.setUserName(people.getUserName());
    				log.setResult("成功");
    			}else{
    				log.setUserName("");
    				log.setResult("失败");
    				//如果是APP访问，放行
    				if(uri.equals("/appLogin.do")
    						||"/maintResume/queryWaitRepairDeviceInter.do".equals(uri)
    						||"/maintResume/queryWaitRepairDeviceInter.do".equals(uri)
    						||"/maintResume/queryAccidentReportInter.do".equals(uri)
    						||"/chart/queryProduct.do".equals(uri)
    						||"/daily/selectDaily2Android.do".equals(uri)
    						||"/disposeRejectsApproval/selectDisposeRejectsList2Android.do".equals(uri)
    						||"/shedu/selectSheduList.do".equals(uri)
    						||"/shedu/selectSheduDetailList.do".equals(uri)
    						||"/shedu/edit2.do".equals(uri)
    						||"/disposeRejectsApproval/androidsubmitApprover.do".equals(uri)
    						||"/approvalRecord/listAllAndroidApprovalRecord.do".equals(uri)
    						||"/pickApply/listAllAndroidPickApplyDetail.do".equals(uri)
    						||"/pickApply/approvalAndroidPickApply.do".equals(uri)
    						||"/reportMana/selectAndroidOutputByTime.do".equals(uri)
    						||"/disposeRejectsApproval/selectMyApprovelListAndroid.do".equals(uri)
    						||"/disposeRejectsApproval/selectMyAlreadyApprovelListAndroid.do".equals(uri)
    						||"/disposeRejects/selectDisposeRejectsDetailsListBydIdAndroid.do".equals(uri)
    						||"/disposeRejectsApproval/selectCopyMeApprovelListAndroid.do".equals(uri)
    						||"/spareParts/queryWarningStock.do".equals(uri)
    						
    						||"/ng/ngList.do".equals(uri)
    						||"/daily/selectDailyByDateAndroid.do".equals(uri)
    						
							||"/breakdown/queryCCPeopleMSG.do".equals(uri)) {
    					log.setResult("成功");
    					log.setUserName("Andriod");
    				}else {
    					//如果web访问没有值，重定向到登录页面
    					response.sendRedirect("/login.jsp");
    				}
    			}
    		}
    		//保存日志信息
    		logManaService.savaLog(log);
        }
	}
	
	/**
	 * 这个方法在controller请求之前调用
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		long startTime = System.currentTimeMillis();
		String address = InetAddress.getLocalHost().getHostAddress().toString();
		String uri = request.getRequestURI();
		//日志记录信息
		User user = (User)request.getSession().getAttribute("currentUser");
		Department dep = (Department)request.getSession().getAttribute("department");
		if (handler instanceof HandlerMethod) {
			HandlerMethod h = (HandlerMethod) handler;
			log.setTime(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(startTime));
			log.setMethod(h.getMethod().getName());
			log.setIpAddress(address);
			
		}
		//web登录，放行
		if("/login.do".equals(uri)){
			return true;
		}
		//app登录，放行
		if("/appLogin.do".equals(uri)){
			return true;
		}
		//app登出，放行
		if("/appLogout.do".equals(uri)){
			return true;
		}
		//如果是APP访问方法，放行
		if("/maintResume/queryWaitRepairDeviceInter.do".equals(uri)||"/maintResume/queryWaitRepairDeviceInter.do".equals(uri)
				||"/maintResume/queryAccidentReportInter.do".equals(uri)||"/chart/queryProduct.do".equals(uri)
				||"/daily/selectDaily2Android.do".equals(uri)||"/disposeRejectsApproval/selectDisposeRejectsList2Android.do".equals(uri)
				||"/shedu/selectSheduList.do".equals(uri)||"/shedu/selectSheduDetailList.do".equals(uri)
				||"/shedu/edit2.do".equals(uri)||"/disposeRejectsApproval/androidsubmitApprover.do".equals(uri)
				||"/approvalRecord/listAllAndroidApprovalRecord.do".equals(uri)||"/pickApply/listAllAndroidPickApplyDetail.do".equals(uri)
				||"/pickApply/approvalAndroidPickApply.do".equals(uri)||"/reportMana/selectAndroidOutputByTime.do".equals(uri)
				||"/disposeRejectsApproval/selectMyApprovelListAndroid.do".equals(uri)
				||"/disposeRejectsApproval/selectMyAlreadyApprovelListAndroid.do".equals(uri)
				||"/disposeRejects/selectDisposeRejectsDetailsListBydIdAndroid.do".equals(uri)
				||"/disposeRejectsApproval/selectCopyMeApprovelListAndroid.do".equals(uri)
				||"/spareParts/queryWarningStock.do".equals(uri)
				||"/ng/ngList.do".equals(uri)
				||"/daily/selectDailyByDateAndroid.do".equals(uri)
				||"/breakdown/queryCCPeopleMSG.do".equals(uri)) {
			return true;
		}
		//如果不是登录，session中是否有值
		if(null != user && null != dep){
	        return true;
		}else{
			//没有值，重定向到登录页面
			response.sendRedirect("/login.jsp");
		}
		return false;
	}
	
}
